//重新开始按钮
var restarBtn = document.querySelector('#restart');
//获取结果元素
var resultArea = document.querySelector('#message');
var winner = document.querySelector('#winner');
//获取单元格元素
var cell = document.querySelector('.cell');
//获取所有单元格
var cells = document.querySelectorAll('.cell');
//添加类名
var cellObj = cell.classList;
var flag = true;
//定义玩家的枚举
var Player;
(function (Player) {
    Player["X"] = "x";
    Player["O"] = "o";
    Player["nextX"] = "readx";
    Player["nextO"] = "reado";
})(Player || (Player = {}));
//类名 
var hoverClass = Player.nextX;
var confirmClass = Player.X;
//单元格 个数计数器
var selectCounts = 0;
//给container 添加点击事件
var container = document.querySelector('.container');
//胜利 的多种情况
var winArr = [
    [0, 1, 2], [3, 4, 5], [6, 7, 8],
    [0, 3, 6], [1, 4, 7], [2, 5, 8],
    [0, 4, 8], [2, 4, 6]
];
//判断胜利的数组
function isWinner(Player) {
    var result = winArr.some(function (item) {
        return item.every(function (cellindex) {
            var cellobj = cells[cellindex];
            return cellobj.classList.contains(Player);
        });
    });
    return (result);
}
function mouse_hover(Event) {
    //给触发事件的对象添加相应的类名
    var target = Event.target;
    // console.log(target)
    target.classList.add(hoverClass);
}
//鼠标移动上预览事件
container.addEventListener('mousemove', mouse_hover);
//鼠标离开 去除预览事件
container.addEventListener('mousemove', function (Event) {
    var target = Event.target;
    target.addEventListener('mouseleave', function () {
        target.classList.remove(hoverClass);
    });
});
//点击确认事件
container.addEventListener('click', function (Event) {
    //给触发事件的对象添加相应的类名
    var target = Event.target;
    //判断触发对象身上是否已经添加过
    if (target.classList.contains('x') || target.classList.contains('o')) {
        return;
    }
    else {
        //按钮 和 游戏区域 标签不需要添加 类名
        if (target.innerHTML !== '重新开始' && !target.classList.contains('game-message')) {
            target.classList.add(confirmClass);
        }
        if (isWinner(confirmClass)) {
            winner.innerHTML = confirmClass + " 赢了";
            resultArea.style.display = 'block';
            //移除相关事件
            container.removeEventListener('mousemove', mouse_hover);
            return;
        }
        flag = !flag;
        //类名
        hoverClass = flag ? Player.nextX : Player.nextO;
        confirmClass = flag ? Player.X : Player.O;
        //选择的单元格个数加1
        selectCounts++;
        //平局判断
        if (selectCounts == 9 && !isWinner(confirmClass)) {
            winner.innerHTML = "平局";
            resultArea.style.display = 'block';
            //移除相关事件
            container.removeEventListener('mousemove', mouse_hover);
            return;
        }
    }
});
//重新开始游戏
restarBtn.addEventListener('click', function () {
    resultArea.style.display = 'none';
    //移除类名
    cells.forEach(function (item) {
        var cell = item;
        cell.classList.remove(Player.X, Player.O, Player.nextO, Player.nextX);
    });
    console.log(restarBtn);
    // //重置计数器
    selectCounts = 0;
    //鼠标移动上预览事件
    container.addEventListener('mousemove', mouse_hover);
});
